package com.guli.mall.pms.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/*
 * @Author 罗
 * @date 2020/10/18 - 9:07 上午
 */
@Component
@Slf4j
@EnableConfigurationProperties(ThreadPoolConfigProperties.class)
public class LocalThreadPoolExecutor {

    @Bean
    public BlockingQueue<Runnable> blockingQueue(){
        return new LinkedBlockingQueue<>(50);
    }

    //@Primary
    @Bean//(name="myExecutorService")
    public ExecutorService threadPoolExecutor(BlockingQueue<Runnable> blockingQueue,ThreadPoolConfigProperties prop){

        RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
        log.info("正在初始化线程池..");
        return new ThreadPoolExecutor(
                prop.getCoreThreadPoolSize(),
                prop.getMaximumPoolSize(),
                prop.getKeepAliveTimeSeconds(),
                TimeUnit.SECONDS,
                blockingQueue,
                Executors.defaultThreadFactory(),
                rejectedExecutionHandler
                );
    }

}
